package com.bop.dehong.setting;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.RegexChk;
import com.base.dehong.util.StringUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.UpdateNicknameModel;
import com.bop.dehong.info.EmptyInfo;
import com.bop.dehong.login.LoginActivity;

/**
 * @author TangWenhan/YeFengyun
 */
public class SetNicknameActivity extends BaseActivity{
	private EditText mNicknameEt;
	private UpdateNicknameModel mUpdateModel;
	private Intent intent;

	@Override
	protected void initTop() {
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle("设置昵称");
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI() {
		intent = getIntent();
		mNicknameEt = getView(R.id.editText1);
		registerOnClickListener(getView(R.id.subimtnick_btn));
	}

	@Override
	protected void initData() {
		
	}
	String nicknameStr;
	
	public void subimt(){
		nicknameStr = getStr4TextView(mNicknameEt);
		if (TextUtils.isEmpty(nicknameStr)) {
			ToastUtils.show(this, "请输入新昵称！");
			return ;
		}
		if (RegexChk.checkNickname(nicknameStr)) {
			String uid = intent.getStringExtra(Config.UID);
			if (TextUtils.isEmpty(uid))
			{
				ToastUtils.show(this, "你还未登录，请先登录！");
				Intent intent = new Intent(this, LoginActivity.class);
				intent.putExtra(Config.GO_BACK, true);
				startActivityForResult(intent, 100);
				return;
			}
			showProgressDialog();
			if (mUpdateModel == null) {
				mUpdateModel = new UpdateNicknameModel(this, callback);
			}
			mUpdateModel.doNet(uid, nicknameStr);
		}else {
			ToastUtils.show(this, "昵称格式不正确！");
		}
	}
	RequestCallBack<EmptyInfo> callback = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(EmptyInfo info) {
			super.onSuccess(info);
			ToastUtils.show(SetNicknameActivity.this, "修改成功！");
			intent.putExtra("nickname", nicknameStr);
			setResult(RESULT_OK,intent);
			finishCurrentActivity();
		}
		
	};
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.act_set_nickname;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.subimtnick_btn:
			subimt();			
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onActivityResult(int arg0, int resultCode, Intent arg2) {
		super.onActivityResult(arg0, resultCode, arg2);
		if (resultCode != RESULT_OK) {
			finishCurrentActivity();
			return;
		}
		subimt();
	}
}

